
<template>
  <div class="asset-inventory-create">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>创建盘点任务</span>
          <el-button @click="handleBack">返回</el-button>
        </div>
      </template>

      <el-form
        ref="taskFormRef"
        :model="taskForm"
        :rules="taskRules"
        label-width="120px"
        class="task-form"
      >
        <el-form-item label="任务类型" prop="taskType">
          <el-radio-group v-model="taskForm.taskType" @change="handleTaskTypeChange">
            <el-radio label="department">部门盘点</el-radio>
            <el-radio label="check">检查盘点</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="taskForm.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="执行部门" prop="department">
          <el-select v-model="taskForm.department" placeholder="请选择执行部门" style="width: 100%">
            <el-option label="财务部" value="finance" />
            <el-option label="行政部" value="admin" />
            <el-option label="技术部" value="tech" />
            <el-option label="市场部" value="marketing" />
          </el-select>
        </el-form-item>
        <el-form-item label="执行人" prop="executor">
          <el-select v-model="taskForm.executor" placeholder="请选择执行人" style="width: 100%">
            <el-option
              v-for="user in userOptions"
              :key="user.id"
              :label="user.name"
              :value="user.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="盘点范围" prop="range">
          <el-checkbox-group v-model="taskForm.range">
            <el-checkbox label="fixed">固定资产</el-checkbox>
            <el-checkbox label="low_value">低值易耗品</el-checkbox>
            <el-checkbox label="lease">租赁资产</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="任务时效" prop="dateRange">
          <el-date-picker
            v-model="taskForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="taskForm.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入备注信息"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitTask">创建任务</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const taskFormRef = ref()

// 表单数据
const taskForm = reactive({
  taskType: 'department',
  taskName: '',
  department: '',
  executor: '',
  range: ['fixed', 'low_value'],
  dateRange: [],
  remark: ''
})

// 表单验证规则
const taskRules = reactive({
  taskType: [
    { required: true, message: '请选择任务类型', trigger: 'change' }
  ],
  taskName: [
    { required: true, message: '请输入任务名称', trigger: 'blur' }
  ],
  department: [
    { required: true, message: '请选择执行部门', trigger: 'change' }
  ],
  executor: [
    { required: true, message: '请选择执行人', trigger: 'change' }
  ],
  range: [
    { type: 'array', required: true, message: '请选择盘点范围', trigger: 'change' }
  ],
  dateRange: [
    { type: 'array', required: true, message: '请选择任务时效', trigger: 'change' }
  ]
})

// 用户选项
const userOptions = ref([
  { id: 1, name: '张三', department: '技术部' },
  { id: 2, name: '李四', department: '行政部' },
  { id: 3, name: '王五', department: '市场部' },
  { id: 4, name: '赵六', department: '财务部' }
])

// 任务类型变化处理
const handleTaskTypeChange = (value) => {
  if (value === 'check') {
    // 检查盘点时，可以选择所有部门
    taskForm.department = ''
  }
}

// 监听执行部门变化，自动过滤执行人
watch(() => taskForm.department, (newVal) => {
  if (newVal) {
    // 根据部门筛选执行人
    // 实际应该从API获取数据
    taskForm.executor = ''
  }
})

// 返回列表
const handleBack = () => {
  router.push('/asset-inventory')
}

// 提交任务
const submitTask = () => {
  taskFormRef.value.validate((valid) => {
    if (valid) {
      // 实际应该调用API保存数据
      console.log('任务表单数据：', taskForm)
      ElMessage.success('创建任务成功')
      router.push('/asset-inventory')
    }
  })
}

// 重置表单
const resetForm = () => {
  taskFormRef.value.resetFields()
  taskForm.range = ['fixed', 'low_value']
}
</script>

<style scoped>
.asset-inventory-create {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.task-form {
  max-width: 600px;
  margin: 0 auto;
}
</style>
